package com.projectsquirrel;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

/*
 * 
 * Day, "[DD]"
Month, "[MM]"
Year, "[YYYY]"
Hour, "[HH]"
Minute, "[MM]"
AMPM, "[XM]"
ZIP, "[XXXXX-XXXX]"
LATITUDE, "[example: 41.86962474217837]"
LONGITUDE, "[example: -87.6494836807251]"
EMAIL, "[XXX@XX.XXX]"
NUM_GRAY_SQUIRRELS, "[X]"
NUM_FOX_SQUIRRELS, "[X]"
SETTING, "[X, where X is one of: SINGLE_FAM, MULT_FAM, HIGHRISE, PARK, NATURE, GARDEN, COMMER, SCHOOL, LOT, CEMETERY, FARM]"
NUT_TREES, "[X, where X is one of: YES, NO, UNSURE]"
SEED_TREES, "[X, where X is one of: YES, NO, UNSURE]"
FRUIT_TREES, "[X, where X is one of: YES, NO, UNSURE]"
TINY_TREES, "[X, where X is one of: YES, NO, UNSURE]"
CON_TREES, "[X, where X is one of: YES, NO, UNSURE]"
OTHER_TREES, "[X, where X is a string]"
FEED_BIRD_FEEDER, "[X, where X is one of NEVER, SELDOM, OFTEN, REGULARLY]"
FEED_HANDOUTS, "[X, where X is one of NEVER, SELDOM, OFTEN, REGULARLY]"
FEED_GARBAGE, "[X, where X is one of NEVER, SELDOM, OFTEN, REGULARLY]"
FEED_TREES, "[X, where X is one of NEVER, SELDOM, OFTEN, REGULARLY]"
FEED_OTHER, "[X, where X is one of NEVER, SELDOM, OFTEN, REGULARLY]"
FEED_OTHER_DETAILS, "[X, where X is a string]"
NUM_SQUIRREL_CHANGE, "[X, where X is one of SAME, INCREASED, DECREASED]"
SITE_DOGS, "[X, where X is one of NONE, LOW, MEDIUM, HIGH]"
SITE_CATS, "[X, where X is one of NONE, LOW, MEDIUM, HIGH]"
SITE_COYOTES, "[X, where X is one of NONE, LOW, MEDIUM, HIGH]"
SITE_HAWKS, "[X, where X is one of NONE, LOW, MEDIUM, HIGH]"
SITE_GRAIN, "[X, where X is one of NONE, LOW, MEDIUM, HIGH]"
THIS_NEW_SITE, "[X, where X is one of YES, NO]"
USED_DIFFERENT_SITE, "[X, where X is one of YES, NO]"
COMMENTS, "[X, where X is a string]"
 * 
 */
public class displayObservation extends Activity  {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.displayobsscreen);
	    
	    //TextView obsText = (TextView) findViewById(R.id.obsText);
	    TextView title = (TextView) findViewById(R.id.textView1);
	    Intent sender=getIntent();
	    final String FileName = sender.getExtras().getString("FileName") + ".obs";
	    Button deleteButton = (Button) findViewById(R.id.DeleteObs);
	    Button cpyButton = (Button) findViewById(R.id.PrintCpy);
	    final StringBuffer buf = new StringBuffer();
	    final String TAG = "Project_2_Group_6";
	    title.setText(FileName.substring(0, 10));
	    try{
	    	InputStream in = openFileInput(FileName);
	    	if(in!=null) {
	    		InputStreamReader tmp = new InputStreamReader(in);
	    		BufferedReader reader= new BufferedReader(tmp);
	    		String str,day,month,year,hour,min,ampm,zip,lat,longi,setting;
	    		day="";
	    		month="";
	    		year="";
	    		hour="";
	    		min="";
	    		ampm="";
	    		zip="";
	    		lat="";
	    		longi="";
	    		setting="";
	    		while((str= reader.readLine()) != null) {
	    			
	    			if(str.contains("ZIP")){
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				zip = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmZIP)).setText(zip);
	    			}
	    			if(str.contains("SETTING")){
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				setting = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmSetting)).setText(setting);
	    			}
	    			if(str.contains("LATITUDE")){
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				lat = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmLatitude)).setText(lat);
	    			}
	    			if(str.contains("LONGITUDE")){
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				longi = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmLongitude)).setText(longi);
	    			}
	    			if(str.contains("Day"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				day = str.substring(index+1, len-1);
	    				

	    			}
	    			if(str.contains("Month"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				month = str.substring(index+1, len-1);
	    				
	    			}

	    			if(str.contains("Year"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				year = str.substring(index+1, len-1);
	    				
	    			}
	    			
	    			String date = month.concat("-");
	    			date = date.concat(day);
	    			date = date.concat("-");
	    			date = date.concat(year);
					((TextView) findViewById(R.id.confirmDate)).setText(date);

	    			if(str.contains("Hour"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				hour = str.substring(index+1, len-1);
	    				int hrs = Integer.parseInt(hour);
	    				if(hrs<10)
	    				{
	    					hour = "0".concat(hour);
	    				}
	    				
	    			}

	    			if(str.contains("Minute"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				min = str.substring(index+1, len-1);
	    				int mins = Integer.parseInt(min);
	    				if(mins<10)
	    				{
	    					min = "0".concat(min);
	    				}
	    				
	    			}

	    			if(str.contains("AMPM"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				ampm = str.substring(index+1, len-1);
	    				
	    			}
	    			
                    
	    			String time = hour.concat(":");
	    			time = time.concat(min);
	    			time = time.concat(" ");
	    			time = time.concat(ampm);
					((TextView) findViewById(R.id.confirmTime)).setText(time);

	    			if(str.contains("NUM_GRAY"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmNumGray)).setText(email);

	    			}

	    			if(str.contains("NUM_FOX"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmNumFox)).setText(email);

	    			}
	    			if(str.contains("EMAIL"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len);
	    				((TextView) findViewById(R.id.confirmEmail)).setText(email);

	    			}
	    			if(str.contains("NUT_"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmNut)).setText(email);

	    			}
	    			if(str.contains("FRUIT_"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmFruit)).setText(email);

	    			}
	    			if(str.contains("SEED_"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmSeed)).setText(email);

	    			}
	    			if(str.contains("TINY_"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmTiny)).setText(email);

	    			}
	    			if(str.contains("TINY_"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmTiny)).setText(email);

	    			}
	    			if(str.contains("CON_"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmCone)).setText(email);

	    			}
	    			if(str.contains("OTHER_TREES"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmOtherTree)).setText(email);

	    			}
	
	    			if(str.contains("FEED_BIRD"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmBird)).setText(email);

	    			}
	    			
	    			if(str.contains("FEED_HANDOUTS"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmHandouts)).setText(email);

	    			}
	    			if(str.contains("FEED_GARBAGE"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmGarbage)).setText(email);

	    			}
	    			if(str.contains("FEED_TREES"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmPlants)).setText(email);

	    			}
	    			if(str.contains("FEED_OTHER") && !(str.contains("DETAILS")))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmOtherFeed)).setText(email);

	    			}

	    			if(str.contains("FEED_OTHER_DETAILS"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmFeedComments)).setText(email);

	    			}
	    			
	    			if(str.contains("NUM_SQUIRREL"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmNumSquirrelChange)).setText(email);

	    			}
	    			if(str.contains("SITE_DOGS"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmDogs)).setText(email);

	    			}
	    			if(str.contains("SITE_CATS"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmCats)).setText(email);

	    			}
	    			if(str.contains("SITE_COYOTES"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmCoyotes)).setText(email);

	    			}
	    			if(str.contains("SITE_HAWKS"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmHawks)).setText(email);

	    			}
	    			if(str.contains("SITE_GRAIN"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmPGrain)).setText(email);

	    			}
	    			if(str.contains("THIS_NEW_SITE"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmSameSite)).setText(email);

	    			}
	    			if(str.contains("USED_DIFFERENT_SITE"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmDiffSite)).setText(email);

	    			}
	    			
	    			if(str.contains("COMMENTS"))
	    			{
	    				int index = str.indexOf('[');
	    				int len = str.length();
	    				String email = str.substring(index+1, len-1);
	    				((TextView) findViewById(R.id.confirmGeneralComments)).setText(email);

	    			}
	    			buf.append(str+"\n");
	    		}
	    		in.close();
	    	
	    	}
	    }
	    catch(java.io.FileNotFoundException e) {
	    	Log.e("Java I/O display OBS", e.toString());
	    }
	    catch(Throwable t) {
	    	Log.e("Java I/O display OBS", t.toString());
	    }
	    
	    deleteButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				deleteFile(FileName);
				finish();
			}	    	
	    });
	    cpyButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Log.i(TAG, buf.toString());
			}	    	
	    });
	}
	
	protected void onStart(){
     	// for the dialog indicating the gps is disabled
		super.onStart();
		/*
 		Calendar calendar = new GregorianCalendar();
 	    int hour = calendar.get(Calendar.HOUR); 		
 	    if(hour>18 || hour<10)
 		{
 			TableLayout l = (TableLayout) findViewById(R.id.Screen);
 			int color=Color.WHITE;
 			l.setBackgroundColor(color);
 			
 		}
 		*/
	}
	
}
